layui.use(['form', 'fsoft'], function () {
  var $ = layui.$
    , form = layui.form,
    fsoft = layui.fsoft;
  var rwid = $('#id').val();
  var intSelectVal = $('#parentId').val();
  if (intSelectVal == "" && rwid != "2849FC29AB744B58A5F9BF5FFEBC2AB5") {
    intSelectVal = "2849FC29AB744B58A5F9BF5FFEBC2AB5";
    $('#parentId').val(intSelectVal);
  }
  var fAction = {
    render: function () {
      fAction.renderOrgTree();
      fAction.renderAreaTree();
    }
    , renderOrgTree: function () {
      $.post(layui.cache['contentPath'] + "/sys-org/findList", {}, function (data) {
        data = eval('(' + data + ')');
        if (data.code == "0") {
          xmSelect.render({
            el: '#xm-select-orgTrees',
            initValue: [$('#parentId').val()],
            prop: { name: 'name', value: 'id' },
            theme: { color: '#0081ff' },
            data: data.data,
            paging: true,
            pageSize: 5,
            clickClose: true,
            radio: true,
            name: "parentId",
            on: function (data) {
              var arr = data.arr;
              if (data.isAdd) {
                $('#parentId').val(arr[0].id);
              } else {
                $('#parentId').val("");
              }
            }
          });
        }
      });
    }
    , renderAreaTree: function () {
      $.post(layui.cache['contentPath'] + "/sys-area/findList", { status: 1, isLeaf: 1 }, function (data) {
        data = eval('(' + data + ')');
        if (data.code == "0") {
          xmSelect.render({
            el: '#fAreaTree'
            , radio: true
            , layVerify: 'required'
            , name: "areaId"
            , initValue: [$('#areaId').val()]
            , prop: { name: 'parentNames', value: 'id' }
            , theme: { color: '#0081ff' }
            , data: data.data
            , filterable: true, clickClose: true, paging: true
            , pageSize: 8
          });
        }
      });
    }
  }
  fAction.render();
  // 监听提交
  form.on('submit(save)', function (data) {
    var submitUrl = layui.cache['contentPath'] + '/sys-org/save';
    if (rwid != undefined && rwid != "") {
      submitUrl = layui.cache['contentPath'] + '/sys-org/modify';
    }
    fsoft.put(submitUrl, data.field, function (rs) {
      if (rs.code == 0) {
        fsoft.successMsg((rwid != undefined && rwid != "") ? "编辑成功" : "新增成功");
        xadmin.close();
      } else {
        fsoft.errorMsg(rs.msg);
      }
    });
    return false;
  });
});